package com.lw.main;

import com.lw.util.Constant;
import com.lw.util.GameUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * 障碍物
 */
@NoArgsConstructor
@Data
public class Barrier {

    private Rectangle rect = new Rectangle();//用于碰撞检测

    private boolean move;//是否移动 true移动

    //障碍图片
    private static BufferedImage imgs[];

    static {
        final int COUNT = Constant.BARRIER_IMG_PATH.length;//图片张数

        //类加载时图片初始化
        imgs = new BufferedImage[COUNT];
        for (int i = 0; i < COUNT; i++) {
            imgs[i] = GameUtil.loadBufferedImage(Constant.BARRIER_IMG_PATH[i]);
        }
    }

    //位置
    private int x, y;

    //宽高
    private int width, height;

    public static final int BARRIER_WIDTH = imgs[0].getWidth();
    public static final int BARRIER_HEIGHT = imgs[0].getHeight();
    public static final int BARRIER_HEAD_WIDTH = imgs[1].getWidth();
    public static final int BARRIER_HEAD_HEIGHT = imgs[1].getHeight();


    //障碍物类型
    private int type;

    //障碍物类型
    public static final int TOP_NORMAL = 0;//自上而下障碍物
    public static final int BOTTOM_NORMAL = 2;//自下而上障碍物
    public static final int HOVER_NORMAL = 4;//悬浮在中间的障碍物
    public static final int MOVEABLE = 6;//悬浮在中间可移动的障碍物

    private final int speed = 20;//障碍物速度

    private boolean visible;//是否在屏幕内

    public Barrier(int x, int y, int height, int type) {
        this.x = x;
        this.y = y;
        this.width = BARRIER_WIDTH;
        this.height = height;
        this.type = type;
    }

    /**
     * 绘制
     *
     * @param g
     */
    public void draw(Graphics g) {
        switch (type) {
            case TOP_NORMAL -> drawTopNormal(g);
            case BOTTOM_NORMAL -> drawNormalTop(g);
            case HOVER_NORMAL -> drawHoverNormal(g);
            case MOVEABLE -> drawMove(g);
        }

        this.drawRect(g);
        x -= speed;
        if (x < -BARRIER_WIDTH) {//判断是否离开了屏幕
            visible = false;
        }
    }

    /**
     * 绘制碰撞矩形
     */
    public void drawRect(Graphics g) {
        int x1 = this.x;
        int y1 = this.y;
        int w1 = BARRIER_WIDTH;
//        g.setColor(Color.blue);
//        g.drawRect(x1, y1, w1, this.height);
        this.setRectangele(x1, y1, w1, height);
    }

    /**
     * 障碍物碰撞矩形参数
     */
    public void setRectangele(int x, int y, int width, int height) {
        rect.x = x;
        rect.y = y;
        rect.width = width;
        rect.height = height;
    }


    /**
     * 绘制从上往下的障碍物
     *
     * @param g
     */
    private void drawTopNormal(Graphics g) {
        //求出所需障碍物的块数 总高度去除掉水管上接头高度/水管每块的高度
        int count = (height - BARRIER_HEAD_HEIGHT) / BARRIER_HEIGHT + 1;

        //绘制障碍物
        for (int i = 0; i < count; i++) {
            g.drawImage(imgs[0], x, y + i * (BARRIER_HEIGHT), null);
        }

        //绘制头
        int y = height - BARRIER_HEAD_HEIGHT;

        g.drawImage(imgs[2], x, y, null);
    }

    /**
     * 绘制从下往上的障碍物
     * 因为后绘制画面覆盖先绘制的，所以要后绘制头
     *
     * @param g
     */
    private void drawNormalTop(Graphics g) {
        //求出所需障碍物的块数
        int count = (height) / BARRIER_HEIGHT + 1;

        //绘制障碍物
        for (int i = 0; i < count; i++) {
            g.drawImage(imgs[0], x, Constant.HEIGHT - i * (BARRIER_HEIGHT), null);
        }

        //绘制头
        int y = Constant.HEIGHT - height;
        g.drawImage(imgs[1], x, y, null);
    }

    /**
     * 绘制悬浮的障碍物
     * @param g
     */
    private void drawHoverNormal(Graphics g) {
        //求出所需障碍物的块数
        int count = (height-BARRIER_HEAD_HEIGHT) / BARRIER_HEIGHT;

        //绘制头
        g.drawImage(imgs[1], x, y, null);

        //绘制障碍物
        for (int i = 0; i < count; i++) {
            g.drawImage(imgs[0], x, y+BARRIER_HEAD_HEIGHT+i * (BARRIER_HEIGHT), null);
        }

        //绘制下面头
        int y = this.y+height-BARRIER_HEAD_HEIGHT;
        g.drawImage(imgs[2], x, y, null);
    }

    /**
     * 绘制自动上下移动的障碍物
     * @param g
     */
    private void drawMove(Graphics g) {
        this.drawHoverNormal(g);

        if(this.move){//控制移动
            this.y+=5;
            if(this.y>=250) move=false;
        }else{
            this.y-=5;
            if(this.y<=100) move = true;
        }
    }

    /**
     * 判断是否需要绘制下一个障碍物
     *
     * @return
     */
    public boolean willCreateNext() {
        return Constant.WIDTH - x > 150;
    }

}
